package com.xdja.netty.server;

import com.xdja.netty.handler.inbound.FilterDecoder;
import com.xdja.netty.handler.inbound.HttpCombinationDecoder;
import com.xdja.netty.handler.inbound.HttpMappingDecoder;
import com.xdja.netty.handler.inbound.ResponseMappingDecoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

/**
 * description
 *
 * @author zk
 * @since 2018/7/23
 */
public class HttpServer {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup bossEventLoopGroup = new NioEventLoopGroup();
        EventLoopGroup worksEventLoopGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        ChannelFuture channelFuture = serverBootstrap.group(bossEventLoopGroup, worksEventLoopGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_KEEPALIVE, false)
                .childHandler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ch.pipeline().addLast()
                                .addLast(new HttpRequestDecoder())
                                .addLast("httpCombinationDecoder", new HttpCombinationDecoder())
                                .addLast("filterDecoder", new FilterDecoder())
                                .addLast("httpMappingDecoder", new HttpMappingDecoder())
                                .addLast("mappingResponseDecoder", new ResponseMappingDecoder())
                                .addLast(new HttpResponseEncoder());
                    }
                }).bind(8989).sync();
        channelFuture.channel().closeFuture().sync();
    }
}
